import { createRouter, createWebHistory } from 'vue-router'
import Splash from '../components/Splash.vue'
import Auth from '../components/Auth.vue'

const routes = [
  {
    path: '/',
    name: 'Splash',
    component: Splash
  },
  {
    path: '/auth',
    name: 'Auth',
    component: Auth
  },
  {
    path: '/dashboard',
    name: 'Dashboard',
    component: () => import('../components/Dashboard.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查是否需要认证
  if (to.meta.requiresAuth) {
    const token = localStorage.getItem('token')
    if (!token) {
      // 没有token，跳转到登录页
      next('/auth')
      return
    }
  }
  
  // 如果已登录用户访问登录页，跳转到首页
  if (to.path === '/auth') {
    const token = localStorage.getItem('token')
    if (token) {
      next('/dashboard')
      return
    }
  }
  
  next()
})

export default router 